package com.example.rabbitmqspringboot.config;/**
 * @program: rabbitmqSpringBoot
 * @author: xlk
 * @create: 2024-04-03 15:33
 */

import com.example.rabbitmqspringboot.constant.BackupConstant;
import com.example.rabbitmqspringboot.constant.ConfirmQueueConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 @auther teacher
 @date 2024/4/3
 @project rabbitmqSpringBoot
 */

@Configuration
public class BackupQueueConfig {
    //声明确认队列,已经声明过了，可以不用重复声明
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(ConfirmQueueConstant.CONFIRM_QUEUE).build();
    }

    //声明备份队列
    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BackupConstant.BACKUP_QUEUE).build();
    }

    //声明告警队列
    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(BackupConstant.WARING_QUEUE).build();
    }

    //声明备份交换机
    @Bean("backupExchange")
    public FanoutExchange backupExchange() {
        return new FanoutExchange(BackupConstant.BACKUP_EXCHANGE);
    }

    //声明确认交换机绑定备份交换机
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(ConfirmQueueConstant.CONFIRM_EXCHANGE)
                .durable(true)
                .withArgument("alternate-exchange", BackupConstant.BACKUP_EXCHANGE)
                .build();
    }

    //绑定确认队列和确认交换机
    @Bean
    public Binding confirmQueueBinding(@Qualifier("confirmQueue") Queue confirmQueue,
                                       @Qualifier("confirmExchange") DirectExchange confirmExchange) {
        return BindingBuilder
                .bind(confirmQueue)
                .to(confirmExchange)
                .with(ConfirmQueueConstant.CONFIRM_ROUTING_KEY);
    }

    //绑定备份队列和备份交换机
    @Bean
    public Binding backupQueueBinding(@Qualifier("backupQueue") Queue backupQueue,
                                      @Qualifier("backupExchange") FanoutExchange backupExchange) {
        return BindingBuilder
                .bind(backupQueue)
                .to(backupExchange);
    }

    //绑定告队列和备份交换机
    @Bean
    public Binding waringQueueBinding(@Qualifier("warningQueue") Queue warningQueue,
                                      @Qualifier("backupExchange") FanoutExchange backupExchange) {
        return BindingBuilder
                .bind(warningQueue)
                .to(backupExchange);
    }



}
